package cn.ibdsr.human.controller;


import cn.ibdsr.human.constant.BizConstants;
import cn.ibdsr.human.constant.Const;
import cn.ibdsr.human.constant.Result;
import cn.ibdsr.human.constant.ServerInitEnum;
import cn.ibdsr.human.db.mapper.NotificationMapper;
import cn.ibdsr.human.db.mapper.ServerInitMapper;
import cn.ibdsr.human.db.model.Notification;
import cn.ibdsr.human.db.model.ServerInit;
import cn.ibdsr.human.exception.BizExceptionEnum;
import cn.ibdsr.human.exception.BusinessException;
import cn.ibdsr.human.manager.LogFormatManager;
import cn.ibdsr.human.service.CompanyRecommendService;
import cn.ibdsr.human.service.INotificationService;
import cn.ibdsr.human.service.JobService;
import cn.ibdsr.human.transfer.ServerInitDTO;
import cn.ibdsr.human.transfer.companyrecommend.CompanyRecommendDTO;
import cn.ibdsr.human.transfer.companyrecommend.CompanyRecommendListDTO;
import cn.ibdsr.human.transfer.companyrecommend.CompanyRecommendQueryDTO;
import cn.ibdsr.human.transfer.job.JobOperatorDTO;
import cn.ibdsr.human.transfer.notification.NotificaitonQueryDTO;
import cn.ibdsr.human.transfer.notification.NotificationDTO;
import cn.ibdsr.human.transfer.notification.NotificationListDTO;
import cn.ibdsr.human.transfer.platform.LatestJobItem;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author cdl
 * @since 2022-03-26
 */
@Api(tags = "通知控制器")
@RequestMapping("/notification")
@RestController
@Slf4j
@Validated
public class NotificationController {
    @Resource
    private NotificationMapper notificationMapper;
    @Resource
    private INotificationService iNotificationService;

    @Resource
    private LogFormatManager logFormatManager;

    /**
     * cdl
     * 获取通知列表
     */
    @ApiOperation("获取通知详情")
    @GetMapping("/no-login/{id}")
    @ResponseBody//
    public Result<NotificationDTO> getNotificaton(@PathVariable("id") Long id  ) {
        logFormatManager.formatKeyPointLog("获取通知详情", JSONObject.toJSONString(id), BizConstants.MODULE_NOTIFICATION);
        NotificationDTO flag = iNotificationService.getNotification(id);
        return new Result<>(flag);
    }
    /**
     * cdl
     * 获取通知列表
     */
    @ApiOperation("获取通知列表")
    @GetMapping("/notifications")
    @ResponseBody//
    public Result<NotificationListDTO> getNotificatons(@Valid NotificaitonQueryDTO notificaitonQueryDTO) {
        logFormatManager.formatKeyPointLog("分页获取通知", JSONObject.toJSONString(notificaitonQueryDTO), BizConstants.MODULE_NOTIFICATION);
        NotificationListDTO flag = iNotificationService.getNotifications(notificaitonQueryDTO);
        return new Result<>(flag);
    }
    /**
     * cdl-ok
     * 平台创建和编辑通知
     */
    @ApiOperation("平台创建通知")
    @PostMapping
    @ResponseBody
    public Result<Long> create(@RequestAttribute(name = Const.USER_ID  ) Long userId,  @RequestBody @Valid NotificationDTO notificationDTO) {

        logFormatManager.formatKeyPointLog("平台创建通知","userId="+userId+":"+ JSONObject.toJSONString(notificationDTO), BizConstants.MODULE_NOTIFICATION);
        Long flag=iNotificationService.create(userId,notificationDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 平台创建和编辑通知
     */
    @ApiOperation("平台编辑通知")
    @PutMapping
    @ResponseBody
    public Result<Long> save(@RequestAttribute(name = Const.USER_ID  ) Long userId, @RequestBody @Valid NotificationDTO notificationDTO) {

        logFormatManager.formatKeyPointLog("平台编辑通知","userId="+userId+":"+ JSONObject.toJSONString(notificationDTO), BizConstants.MODULE_NOTIFICATION);
        Long flag=iNotificationService.save(userId,notificationDTO);
        return new Result<>(flag);
    }



    /**
     * cdl-ok
     * 平台发布/取消发布通知
     */
    @ApiOperation("平台发布/取消发布通知")
    @PutMapping("/notification-statuses/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> publishOrNot( @RequestAttribute(name = Const.USER_ID  ) Long userId,@Valid @PathVariable("id") Long id,   @RequestBody JobOperatorDTO jobSaveDTO) {

        jobSaveDTO.setId(id);
        logFormatManager.formatKeyPointLog("发布/取消发布","userId="+userId+":"+JSONObject.toJSONString(jobSaveDTO), BizConstants.MODULE_NOTIFICATION);
        Long flag=iNotificationService.publishOrNot(userId,jobSaveDTO);
        return new Result<>(flag);
    }

    /**
     * cdl-ok
     * 删除通知
     */
    @ApiOperation("删除通知")
    @DeleteMapping("/notifications/{id}")//@RequestAttribute(name = Const.USER_ID  )
    @ResponseBody
    public Result<Long> delete( @RequestAttribute(name = Const.USER_ID  ) Long userId,@Valid @PathVariable("id") Long id) {

        logFormatManager.formatKeyPointLog("删除通知","userId="+userId+": id="+id, BizConstants.MODULE_NOTIFICATION);
        Long flag=iNotificationService.delete(userId,id);
        return new Result<>(flag);
    }

}

